package com.lzx.iteam.skin;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.*;
import android.util.Log;
import android.widget.ImageView;
import com.lzx.iteam.CloudDialerActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.*;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.SkinUtil;
import com.lzx.iteam.util.StringUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.util.ArrayList;

/**
 * Created by hfx on 15-1-4.
 */
public class GetSkinResource {

    public UnZip mZip;
    public File mFileUtil;
    private static String TAG = "GetSkinResource";
    private SkinPropertys mProperty;

    public GetSkinResource(Context context) {
        mZip = new UnZip(context);
    }

    public ArrayList<SkinDirData> mDirDatas;

    public ArrayList<SkinDirData> getSkinDir(Context context) {
        ArrayList<SkinDirData> result = new ArrayList<SkinDirData>();
        result.add(addLocalSkin(context));
        File dir = context.getFilesDir();
        File key = new File(dir.getAbsolutePath() + "/" + "skin");
        if (key.exists()) {
            ArrayList<String> names = mZip.readFile(key);
            for (String name : names) {
                File indir = new File(name);
                if (indir.exists()) {
                    SkinDirData data = new SkinDirData();
                    if (indir.isDirectory()) {
                        data.dirName = name;
                        File plist = new File(name + File.separator + "colors.plist");
                        if (plist.exists()) {
                            try {
                                data.skinName = getSkinName(plist);
                            } catch (ParserConfigurationException e) {
                                e.printStackTrace();
                            } catch (SAXException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        File iconFile = new File(name + File.separator + "star_h.png");
                        if (iconFile.exists()) {
                            data.skinIcon = getSkinIcon(name + File.separator + "star_h.png");
                        }
                    }
                    result.add(data);
                }
            }
        }
        return result;
    }

    private SkinDirData addLocalSkin(Context context) {
        SkinDirData data = new SkinDirData();
        data.dirName = "local";
        data.skinIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ios_star_h);
        data.skinName = "IOS7扁平风格";
        return data;
    }

    public SkinCommonData loadCommon(Context context, String skinPath, int sound) {
        File file = new File(skinPath);
        SkinCommonData skin;
        if (!file.exists()) {
            skin = loadDefaultSkinGloble(context);
        } else {
            skin = loadSkinGloble(context, skinPath);
        }

        if (sound == Constants.SOUND_THEME) {
            getMusic(context, skinPath, skin);
            skin.hasmusic = Constants.SOUND_THEME;
        } else if (sound == Constants.SOUND_DEFAULT) {
            getDefaultMusic(context, skin);
            skin.hasmusic = Constants.SOUND_DEFAULT;
        } else if (sound == Constants.SOUND_CLOSE) {
            skin.hasmusic = Constants.SOUND_CLOSE;
            destoryMusic(skin);
        }
        return skin;
    }

    public SkinVertyData getAllVertSkinData(Context context, String skinPath) {
        SkinVertyData result = new SkinVertyData();
        File file = new File(skinPath);
        if (file.exists()) {
            mProperty = getSkinProperties(skinPath);
            //Vert公共区域
            loadVertCommonPic(context, skinPath, result, mProperty);
            // vT9键盘
            loadVertT9Pic(context, skinPath, result);
            // vABC键盘
            loadSkinVertQwertyPic(context, skinPath, result);
        } else {
            mProperty = getDefaultPropertys(context, R.raw.ios_colors);
            //Default Vert公共区域
            loadDefaultVertCommonPic(context, result);
            // vT9键盘
            loadDefaultVertT9Pic(context, result);
            // vABC键盘
            loadDefaultVertQwePic(context, result);
        }
        return result;
    }

    public SkinVertyData getVertT9SkinData(SkinVertyData data, Context context, String skinPath) {
        File file = new File(skinPath);
        if (file.exists()) {
            mProperty = getSkinProperties(skinPath);
            //Vert公共区域
            loadVertCommonPic(context, skinPath, data, mProperty);
            // vT9键盘
            loadVertT9Pic(context, skinPath, data);
            // vABC键盘
        } else {
            mProperty = getDefaultPropertys(context, R.raw.ios_colors);
            //Default Vert公共区域
            loadDefaultVertCommonPic(context, data);
            // vT9键盘
            loadDefaultVertT9Pic(context, data);
        }
        return data;
    }

    public SkinVertyData getVertABCSkinData(SkinVertyData data, Context context, String skinPath) {
        File file = new File(skinPath);
        if (file.exists()) {
            mProperty = getSkinProperties(skinPath);
            //Vert公共区域
            loadVertCommonPic(context, skinPath, data, mProperty);
            // vABC键盘
            loadSkinVertQwertyPic(context, skinPath, data);
        } else {	
            mProperty = getDefaultPropertys(context, R.raw.ios_colors);
            //Default Vert公共区域
            loadDefaultVertCommonPic(context, data);
            // vT9键盘
            loadDefaultVertQwePic(context, data);
        }
        return data;
    }

    public SkinHorizData getAllHorizSkinData(Context context, String skinPath) {
        SkinHorizData result = new SkinHorizData();
        File file = new File(skinPath);
        if (file.exists()) {
            mProperty = getSkinProperties(skinPath);
            //Horiz 公共区域
            loadSkinHoriCommonPic(context, skinPath, result, mProperty);
            //hT9键盘
            loadSkinHoriT9Pic(context, skinPath, result);
        } else {
            mProperty = getDefaultPropertys(context, R.raw.ios_colors);
            loadDefaultHorizCommonPic(context, result);
            loadDefaultHorizT9Pic(context, result);
        }
        return result;
    }

    private void loadSkinHoriCommonPic(Context context, String skinPath, SkinHorizData result, SkinPropertys propertys) {
        Resources res = context.getResources();
        Drawable tempDrawable = null;

        result.hInputResId = getPathNormalDrawable("imginput_l", skinPath, null, propertys.tintColor);

        tempDrawable = getPathNormalDrawable("abc_t9", skinPath, "button_bak", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("button_pressed", "drawable", "com.lzx.iteam");
            result.hAndAbcResId = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }

        tempDrawable = getPathNormalDrawable("hide_t9", skinPath, "button_bak", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("button_pressed", "drawable", "com.lzx.iteam");
            result.hidet9 = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }
    }

    private void loadDefaultHorizCommonPic(Context context, SkinHorizData result) {
        Resources res = context.getResources();

        result.hInputResId = res.getDrawable(R.drawable.ios_imginput_l);

        int id = R.drawable.ios_abc_t9;
        int pressid = R.drawable.button_pressed;
        result.hAndAbcResId = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));

        id = R.drawable.ios_hide_t9;
        result.hidet9 = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));
    }

    public void loadSkinHoriT9Pic(Context context, String skinPath, SkinHorizData result) {
        Resources res = context.getResources();
        Drawable tempDrawable = null;

        for (int i = 0; i < result.ht9BgResId.length; i++) {
            int pressid = res.getIdentifier("v" + i + "_pressed", "drawable", "com.lzx.iteam");
            tempDrawable = bitmapToDrawable(getSkinIcon(skinPath + "/" + "a" + i + ".png"));
            if (tempDrawable != null) {
                result.ht9BgResId[i] = setBackground(res, res.getDrawable(pressid), tempDrawable);
            }
        }

        tempDrawable = getPathNormalDrawable("star_h", skinPath, "star_v", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("vstar_pressed", "drawable", "com.lzx.iteam");
            result.hstarResId = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }

        tempDrawable = getPathNormalDrawable("del_h", skinPath, "del_v", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("vdel_pressed", "drawable", "com.lzx.iteam");
            result.hdelResId = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }

        tempDrawable = getPathNormalDrawable("call_h", skinPath, "call_v", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("vcall_pressed", "drawable", "com.lzx.iteam");
            result.hcallResId = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }

        tempDrawable = getPathNormalDrawable("sms_h", skinPath, "sms_v", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("vsms_pressed", "drawable", "com.lzx.iteam");
            result.hsmsResId = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }
    }

    private void loadDefaultHorizT9Pic(Context context, SkinHorizData result) {
        Resources res = context.getResources();
        for (int i = 0; i < result.ht9BgResId.length; i++) {
            int id = res.getIdentifier("ios_a" + i, "drawable", "com.lzx.iteam");
            int pressid = res.getIdentifier("v" + i + "_pressed", "drawable", "com.lzx.iteam");
            result.ht9BgResId[i] = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));
        }
        int id = R.drawable.ios_star_h;
        int pressid = R.drawable.vstar_pressed;
        result.hstarResId = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));

        id = R.drawable.ios_del_h;
        pressid = R.drawable.vdel_pressed;
        result.hdelResId = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));

        id = R.drawable.ios_call_h;
        pressid = R.drawable.vcall_pressed;
        result.hcallResId = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));

        id = R.drawable.ios_sms_h;
        pressid = R.drawable.vsms_pressed;
        result.hsmsResId = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));
    }

    private SkinCommonData loadSkinGloble(Context context, String skinPath) {
        Resources res = context.getResources();
        SkinCommonData result = SkinCommonData.getInstance();
        mProperty = getSkinProperties(skinPath);
        result.scaleType = ImageView.ScaleType.FIT_XY; // where is the resources?
        Drawable tempDrawable = null;

        // 主界面 标题 一长条的背景
        tempDrawable = bitmapToDrawable(getSkinIcon(skinPath + "/" + "imgtint" + ".png"));
        if (tempDrawable == null) {
            int r = Color.red(mProperty.tintColor);
            int g = Color.green(mProperty.tintColor);
            int b = Color.blue(mProperty.tintColor);
            int end = Color.rgb(r + 35, g + 35, b + 35);
            int begin = Color.rgb(r - 10, g - 10, b - 10);
            GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP,
                    new int[]{
                            begin, end
                    });
            gd.setGradientType(GradientDrawable.LINEAR_GRADIENT);
            tempDrawable = gd;
        }
        result.mainTitleBgResid = tempDrawable;

        // init colors
        result.tintColor = mProperty.tintColor;
        result.barTitleColor = mProperty.barTitleColor;
        result.nameColor = mProperty.nameColor;
        result.jobTitleColor = mProperty.jobTitleColor;
        result.phoneColor = mProperty.phoneColor;
        result.backgroundColor = mProperty.backgroundColor;
        result.listBgColor = mProperty.listBgColor;
        result.listDividerColor = mProperty.listDividerColor;
        result.listSectionColor = mProperty.listSectionColor;
        result.listSectionColor = mProperty.listSectionColor;
        result.listTextHightLightColor = mProperty.listTextHightLightColor;

        // 联系人列表的图片背景
        result.listBgBitmap = getPathNormalDrawable("listbackground", skinPath, null, -1);

        // 默认联系人头像图片资源
        result.noNameBitmap = getPathNormalDrawable("noname", skinPath, null, -1);

        // top button colors
        float[] tabLef = new float[]{12f, 12f, 0f, 0f, 0f, 0f, 12f, 12f};
        float[] tabRight = new float[]{0f, 0f, 12f, 12f, 12f, 12f, 0f, 0f};
        result.left = tabButtonBg(tabLef);
        result.right = tabButtonBg(tabRight);
        result.center = tabButtonBg(tabLef);
        return result;
    }


    private SkinCommonData loadDefaultSkinGloble(Context context) {
        int propertyId = R.raw.ios_colors;
        Resources res = context.getResources();
        SkinCommonData result = SkinCommonData.getInstance();
        if (propertyId != 0) {
            mProperty = getDefaultPropertys(context, propertyId);
        }
        // int scaltypeId = newres.getIdentifier(prefix + "scaletype", "raw", newPackageName);
        result.scaleType = ImageView.ScaleType.FIT_XY;
        int id = R.drawable.ios_imgtint;
        result.mainTitleBgResid = res.getDrawable(id);
//        id = newres.getIdentifier(prefix + "listbackground", "drawable", newPackageName);
        result.listBgBitmap = null;
        id = R.drawable.ios_noname;
        result.noNameBitmap = res.getDrawable(id);
        result.tintColor = mProperty.tintColor;
        result.barTitleColor = mProperty.barTitleColor;
        result.nameColor = mProperty.nameColor;
        result.jobTitleColor = mProperty.jobTitleColor;
        result.phoneColor = mProperty.phoneColor;
        result.backgroundColor = mProperty.backgroundColor;
        result.listBgColor = mProperty.listBgColor;
        result.listDividerColor = mProperty.listDividerColor;
        result.listSectionColor = mProperty.listSectionColor;
        result.listSectionColor = mProperty.listSectionColor;
        result.listTextHightLightColor = mProperty.listTextHightLightColor;
        float[] tabLef = new float[]{12f, 12f, 0f, 0f, 0f, 0f, 12f, 12f};
        float[] tabRight = new float[]{0f, 0f, 12f, 12f, 12f, 12f, 0f, 0f};
        result.left = tabButtonBg(tabLef);
        result.right = tabButtonBg(tabRight);
        result.center = tabButtonBg(tabLef);
        return result;
    }

    /**
     * 加载默认皮肤,在工程的drawable目录下，0-9数字键的背景命令分别为ios_f1,ios_f2...v1_pressed,v2_pressed,对应的是每个按键的普通和按下状态
     * 通过getIdentifier获得到每个图片的id,通过StateListDrawable数据将每个按键的资源ID保存
     *
     * @param context
     * @param result
     */
    private void loadDefaultVertT9Pic(Context context, SkinVertyData result) {
        Resources res = context.getResources();
        //0-9数字键的背景图片，分为普通和按下状态
        for (int i = 0; i < result.vt9BgResId.length; i++) {
            int id = res.getIdentifier("ios_f" + i, "drawable", "com.lzx.iteam");
            int pressid = res.getIdentifier("v" + i + "_pressed", "drawable", "com.lzx.iteam");
            result.vt9BgResId[i] = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));
        }

        int id = R.drawable.ios_star_v;
        int pressid = R.drawable.vstar_pressed;
        result.vstarResId = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));

        id = R.drawable.ios_del_v;
        pressid = R.drawable.vdel_pressed;
        result.vt9delResId = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));

        id = R.drawable.ios_call_v;
        pressid = R.drawable.vcall_pressed;
        result.vcallResId = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));

        id = R.drawable.ios_sms_v;
        pressid = R.drawable.vsms_pressed;
        result.vsmsResId = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));

        id = R.drawable.ios_imginput_s;
        result.vInputResId = res.getDrawable(id);

//        id = R.drawable.ios_abc_zm;
        if (CloudDialerActivity.isShowChange) {
            id = R.drawable.ios_abc_zm_change;
        } else {
            id = R.drawable.ios_abc_zm_add;
        }

        result.vAndAbcResId_normal = res.getDrawable(id);
    }

    private void loadDefaultVertQwePic(Context context, SkinVertyData result) {
        String abc = "abcdefghijklmnopqrstuvwxyz";
        result.needSetText.clear();
        Resources res = context.getResources();
        for (int i = 0; i < result.abcBgResId.length; i++) {
            int id = res.getIdentifier("ios_" + abc.charAt(i), "drawable", "com.lzx.iteam");
            int pressid = res.getIdentifier("v" + abc.charAt(i) + "_pressed", "drawable", "com.lzx.iteam");
            result.abcBgResId[i] = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));
        }
        // vABC键盘 "delete"健
        int id = R.drawable.ios_del_h_zm;
        int pressid = R.drawable.vdel_pressed;
        result.vabcdelResId = setBackground(res, res.getDrawable(pressid), res.getDrawable(id));
    }

    public void loadDefaultVertCommonPic(Context context, SkinVertyData result) {
        Resources res = context.getResources();
        int id = R.drawable.ios_imginput_s;
        result.vInputResId = res.getDrawable(id);
//        id = R.drawable.ios_abc_zm;
        if (CloudDialerActivity.isShowChange) {
            id = R.drawable.ios_abc_zm_change;
        } else {
            id = R.drawable.ios_abc_zm_add;
        }

        result.vAndAbcResId_normal = res.getDrawable(id);
    }
    


    


    public void getMusic(Context context, String skinPath, SkinCommonData result) {
        File tempFile = null;
        String tempPath = null;
        Resources res = context.getResources();
        // t9数字键盘音频文件
        for (int i = 0; i < result.t9mp3.length; i++) {
            tempPath = skinPath + File.separator + "dtmf_" + i + ".mp3";
            tempFile = new File(tempPath);
            if (!tempFile.exists()) {
                int id = res.getIdentifier("dtmf_" + i, "raw", "com.lzx.iteam");
                result.def_t9mp3[i] = res.openRawResourceFd(id);
            } else {
                result.t9mp3[i] = tempPath;
            }
        }

        tempPath = skinPath + File.separator + "lock.mp3";
        tempFile = new File(tempPath);
        if (!tempFile.exists()) {
            int id = res.getIdentifier("lock", "raw", "com.lzx.iteam");
            result.def_lock = res.openRawResourceFd(id);
        } else {
            result.lock = tempPath;
        }

        tempPath = skinPath + File.separator + "tink.mp3";
        tempFile = new File(tempPath);
        if (!tempFile.exists()) {
            int id = res.getIdentifier("tink", "raw", "com.lzx.iteam");
            result.def_tink = res.openRawResourceFd(id);
        } else {
            result.tink = tempPath;
        }

        tempPath = skinPath + File.separator + "tock.mp3";
        tempFile = new File(tempPath);
        if (!tempFile.exists()) {
            int id = res.getIdentifier("tock", "raw", "com.lzx.iteam");
            result.def_tock = res.openRawResourceFd(id);
        } else {
            result.tock = tempPath;
        }

        tempPath = skinPath + File.separator + "ussd.mp3";
        tempFile = new File(tempPath);
        if (!tempFile.exists()) {
            int id = res.getIdentifier("ussd", "raw", "com.lzx.iteam");
            result.def_ussd = res.openRawResourceFd(id);
        } else {
            result.ussd = tempPath;
        }

        tempPath = skinPath + File.separator + "dtmf_pound.mp3";
        tempFile = new File(tempPath);
        if (!tempFile.exists()) {
            int id = res.getIdentifier("dtmf_pound", "raw", "com.lzx.iteam");
            result.def_pound = res.openRawResourceFd(id);
        } else {
            result.pound = tempPath;
        }

        tempPath = skinPath + File.separator + "dtmf_star.mp3";
        tempFile = new File(tempPath);
        if (!tempFile.exists()) {
            int id = res.getIdentifier("dtmf_star", "raw", "com.lzx.iteam");
            result.def_star = res.openRawResourceFd(id);
        } else {
            result.star = tempPath;
        }

        // abc键盘音频文件
        String abc = "abcdefghijklmnopqrstuvwxyz";
        for (int i = 0; i < result.abcmp3.length; i++) {
            String path = "dtmf_" + abc.charAt(i) + ".mp3";
            tempFile = new File(path);
            if (!tempFile.exists()) {
                path = result.tock;
                if (!StringUtil.isEmpty(path)) {
                    tempFile = new File(path);
                } else {
                    result.def_abcmp3[i] = result.def_tock;
                    continue;
                }
                if (!tempFile.exists()) {
                    result.def_abcmp3[i] = result.def_tock;
                } else {
                    result.abcmp3[i] = result.tock;
                }
            } else {
                result.abcmp3[i] = path;
            }
        }
    }

    public void getDefaultMusic(Context context, SkinCommonData result) {
        Resources res = context.getResources();

        for (int i = 0; i < result.def_t9mp3.length; i++) {
            int id = res.getIdentifier("dtmf_" + i, "raw", "com.lzx.iteam");
            result.def_t9mp3[i] = res.openRawResourceFd(id);
        }
        result.def_lock = res.openRawResourceFd(R.raw.lock);
        result.def_tink = res.openRawResourceFd(R.raw.tink);
        result.def_tock = res.openRawResourceFd(R.raw.tock);
        result.def_ussd = res.openRawResourceFd(R.raw.ussd);
        for (int i = 0; i < result.def_abcmp3.length; i++) {
            result.def_abcmp3[i] = result.def_tock;
        }
        result.def_pound = res.openRawResourceFd(R.raw.dtmf_pound);
        result.def_star = res.openRawResourceFd(R.raw.dtmf_star);
    }

    public void loadVertCommonPic(Context context, String skinPath, SkinVertyData result, SkinPropertys property) {
        // vT9 vABC 键盘输入框背景
        Resources res = context.getResources();
        result.vInputResId = getPathNormalDrawable("imginput_s", skinPath, "imginput_s_i5", property.tintColor);
        // vT9 "+" 号键背景
        Drawable tempDrawable = getPathNormalDrawable("abc_zm", skinPath, "button_bak", -1);
        int pressid = res.getIdentifier("button_pressed", "drawable", "com.lzx.iteam");
        result.vAndAbcResId_normal = setBackground(res, res.getDrawable(pressid), tempDrawable);
    }

    public void loadVertT9Pic(Context context, String skinPath, SkinVertyData result) {
        Resources res = context.getResources();
        Drawable tempDrawable;
        for (int i = 0; i < result.vt9BgResId.length; i++) {
            int pressid = res.getIdentifier("v" + i + "_pressed", "drawable", "com.lzx.iteam");
            tempDrawable = bitmapToDrawable(getSkinIcon(skinPath + "/" + "f" + i + ".png"));
            if (tempDrawable == null) {
                tempDrawable = bitmapToDrawable(getSkinIcon(skinPath + "/" + "f" + i + "_i5" + ".png"));
            }
            if (tempDrawable != null) {
                result.vt9BgResId[i] = setBackground(res, res.getDrawable(pressid), tempDrawable);
            }
        }

        // vT9键盘 "*"健
        tempDrawable = null;
        tempDrawable = getPathNormalDrawable("star_v", skinPath, "star_h", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("vstar_pressed", "drawable", "com.lzx.iteam");
            result.vstarResId = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }


        // vT9键盘 "delete"健
        tempDrawable = null;
        tempDrawable = getPathNormalDrawable("del_v", skinPath, "del_h", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("vdel_pressed", "drawable", "com.lzx.iteam");
            result.vt9delResId = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }

        // vT9键盘 "call"健
        tempDrawable = null;
        tempDrawable = getPathNormalDrawable("call_v", skinPath, "call_h", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("vcall_pressed", "drawable", "com.lzx.iteam");
            result.vcallResId = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }

        // vT9键盘 "sms"健
        tempDrawable = null;
        tempDrawable = getPathNormalDrawable("sms_v", skinPath, "sms_h", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("vsms_pressed", "drawable", "com.lzx.iteam");
            result.vsmsResId = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }
    }

    public void loadSkinVertQwertyPic(Context context, String skinPath, SkinVertyData result) {
        String abc = "abcdefghijklmnopqrstuvwxyz";
        result.needSetText.clear();
        Resources res = context.getResources();
        Drawable tempDrawable;
        for (int i = 0; i < result.abcBgResId.length; i++) {
            char c = abc.charAt(i);
            tempDrawable = bitmapToDrawable(getSkinIcon(skinPath + "/" + c + ".png"));
            if (tempDrawable == null) {
                tempDrawable = bitmapToDrawable(getSkinIcon(skinPath + "/" + c + "_i5" + ".png"));
            }
            if (tempDrawable == null) {
                tempDrawable = bitmapToDrawable(getSkinIcon(skinPath + "/" + "button_bak.png"));
                if (tempDrawable == null) {
                    tempDrawable = bitmapToDrawable(getSkinIcon(skinPath + "/" + "button_bak_i5.png"));
                }
                int pressid = res.getIdentifier("button_pressed", "drawable", "com.lzx.iteam");
                result.abcBgResId[i] = setBackground(res, res.getDrawable(pressid), tempDrawable);
                result.needSetText.add(abc.charAt(i) + "");
            } else {
                int pressid = res.getIdentifier("v" + abc.charAt(i) + "_pressed", "drawable", "com.lzx.iteam");
                result.abcBgResId[i] = setBackground(res, res.getDrawable(pressid), tempDrawable);
            }
        }

        // vABC键盘 "delete"健
        tempDrawable = null;
        tempDrawable = getPathNormalDrawable("del_h_zm", skinPath, "del_h", -1);
        if (tempDrawable != null) {
            int pressid = res.getIdentifier("vdel_pressed", "drawable", "com.lzx.iteam");
            result.vabcdelResId = setBackground(res, res.getDrawable(pressid), tempDrawable);
        }

    }

    private Drawable getPathNormalDrawable(String fileName, String skinPath, String defaultName, int color) {
        Drawable result = null;
        result = bitmapToDrawable(getSkinIcon(skinPath + "/" + fileName + ".png"));
        if (result == null) {
            result = bitmapToDrawable(getSkinIcon(skinPath + "/" + fileName + "_i5.png"));
            if (result == null) {
                if (!StringUtil.isEmpty(defaultName)) {
                    result = bitmapToDrawable(getSkinIcon(skinPath + "/" + defaultName + ".png"));
                    if (result == null) {
                        if (color != -1)
                            result = new ColorDrawable(color);
                    }
                } else {
                    if (color != -1)
                        result = new ColorDrawable(color);
                }
            }
        }
        return result;
    }

    public static StateListDrawable setBackground(Resources res, Drawable pressed, Drawable normal) {
        StateListDrawable bg = new StateListDrawable();
        bg.addState(new int[]{android.R.attr.state_pressed}, pressed);
        bg.addState(new int[]{}, normal);
        return bg;
    }


    public SkinPropertys getSkinProperties(String skinPath) {
        SkinPropertys result = null;
        File file = new File(skinPath);
        if (file.exists()) {
            if (file.isDirectory()) {
                File propertyFile = new File(skinPath + File.separator + "colors.plist");
                if (propertyFile.exists()) {
                    result = new SkinPropertys();
                    try {
                        InputStream is = new FileInputStream(propertyFile);
                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                        DocumentBuilder builder = factory.newDocumentBuilder();
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        byte[] bytes = new byte[1024];
                        int len = 0;
                        while ((len = is.read(bytes)) > 0) {
                            bos.write(bytes, 0, len);
                            bos.flush();
                        }
                        String content = bos.toString("utf-8");
                        is.close();
                        bos.close();
                        content = SkinUtil.filter(content);
                        ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes("utf-8"));
                        Document doc = builder.parse(bis);
                        NodeList list = doc.getElementsByTagName("key");
                        NodeList arrs = doc.getElementsByTagName("array");
                        int j = 0;
                        for (int i = 0; i < list.getLength(); i++) {
                            Element element = (Element) list.item(i);
                            String key = element.getFirstChild().getNodeValue();
                            if (key.equals("NameColor")) {
                                Element arrElement = (Element) arrs.item(j);
                                j++;
                                result.nameColor = parseColorProperty(arrElement);
                            } else if (key.equals("JobTitleColor")) {
                                Element arrElement = (Element) arrs.item(j);
                                j++;
                                result.jobTitleColor = parseColorProperty(arrElement);
                            } else if (key.equals("PhoneColor")) {
                                Element arrElement = (Element) arrs.item(j);
                                j++;
                                result.phoneColor = parseColorProperty(arrElement);
                            } else if (key.equals("BackgroundColor")) {
                                Element arrElement = (Element) arrs.item(j);
                                j++;
                                result.backgroundColor = parseColorProperty(arrElement);
                            } else if (key.equals("TintColor")) {
                                Element arrElement = (Element) arrs.item(j);
                                j++;
                                result.tintColor = parseColorProperty(arrElement);
                            } else if (key.equals("BarTitleColor")) {
                                Element arrElement = (Element) arrs.item(j);
                                j++;
                                result.barTitleColor = parseColorProperty(arrElement);
                            } else if (key.equals("ThemeName")) {
                                Element theme = (Element) doc.getElementsByTagName("string").item(0);
                                result.themeName = theme.getFirstChild().getNodeValue();
                            } else if (key.equals("SeparatorColor")) {
                                Element arrElement = (Element) arrs.item(j);
                                j++;
                                result.listDividerColor = parseColorProperty(arrElement);
                            } else if (key.equals("ContrastColor")) {
                                Element arrElement = (Element) arrs.item(j);
                                j++;
                                result.listTextHightLightColor = parseColorProperty(arrElement);
                            } else if (key.equals("ListBackgroundColor")) {
                                Element arrElement = (Element) arrs.item(j);
                                j++;
                                result.listBgColor = parseColorProperty(arrElement);
                            } else if (key.equals("SectionHeaderBackgroundColor")) {
                                Element arrElement = (Element) arrs.item(j);
                                j++;
                                result.listSectionColor = parseColorProperty(arrElement);
                            }
                        }
                        bis.close();
                        bis = null;
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (ParserConfigurationException e) {
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (SAXException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return result;
    }

    public SkinPropertys getDefaultPropertys(Context context, int id) {
        Resources res = context.getResources();
        SkinPropertys result = new SkinPropertys();
        InputStream is = res.openRawResource(id);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = is.read(bytes)) > 0) {
                bos.write(bytes, 0, len);
                bos.flush();
            }
            String content = bos.toString("utf-8");
            is.close();
            bos.close();
            content = SkinUtil.filter(content);
            ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes("utf-8"));
            Document doc = builder.parse(bis);
            NodeList list = doc.getElementsByTagName("key");
            NodeList arrs = doc.getElementsByTagName("array");
            int j = 0;
            for (int i = 0; i < list.getLength(); i++) {
                Element element = (Element) list.item(i);
                String key = element.getFirstChild().getNodeValue();
                if (key.equals("NameColor")) {
                    Element arrElement = (Element) arrs.item(j);
                    j++;
                    result.nameColor = parseColorProperty(arrElement);
                } else if (key.equals("JobTitleColor")) {
                    Element arrElement = (Element) arrs.item(j);
                    j++;
                    result.jobTitleColor = parseColorProperty(arrElement);
                } else if (key.equals("PhoneColor")) {
                    Element arrElement = (Element) arrs.item(j);
                    j++;
                    result.phoneColor = parseColorProperty(arrElement);
                } else if (key.equals("BackgroundColor")) {
                    Element arrElement = (Element) arrs.item(j);
                    j++;
                    result.backgroundColor = parseColorProperty(arrElement);
                } else if (key.equals("TintColor")) {
                    Element arrElement = (Element) arrs.item(j);
                    j++;
                    result.tintColor = parseColorProperty(arrElement);
                } else if (key.equals("BarTitleColor")) {
                    Element arrElement = (Element) arrs.item(j);
                    j++;
                    result.barTitleColor = parseColorProperty(arrElement);
                } else if (key.equals("ThemeName")) {
                    Element theme = (Element) doc.getElementsByTagName("string").item(0);
                    result.themeName = theme.getFirstChild().getNodeValue();
                } else if (key.equals("SeparatorColor")) {
                    Element arrElement = (Element) arrs.item(j);
                    j++;
                    result.listDividerColor = parseColorProperty(arrElement);
                } else if (key.equals("ContrastColor")) {
                    Element arrElement = (Element) arrs.item(j);
                    j++;
                    result.listTextHightLightColor = parseColorProperty(arrElement);
                } else if (key.equals("ListBackgroundColor")) {
                    Element arrElement = (Element) arrs.item(j);
                    j++;
                    result.listBgColor = parseColorProperty(arrElement);
                } else if (key.equals("SectionHeaderBackgroundColor")) {
                    Element arrElement = (Element) arrs.item(j);
                    j++;
                    result.listSectionColor = parseColorProperty(arrElement);
                }
            }
            bis.close();
            bis = null;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    private String getFileName(String path) {
        mFileUtil = new File(path);
        if (mFileUtil.exists()) {
            return mFileUtil.getName();
        }
        return null;
    }


    private Drawable bitmapToDrawable(Bitmap bitmap) {
        if (bitmap == null) return null;
        Drawable result = new BitmapDrawable(bitmap);
        Log.d(TAG, "bitmap is " + bitmap.toString());
//        bitmap.recycle();
        return result;
    }

    public int parseColorProperty(Element arrElement) {
        if (arrElement == null) {
            return 0;
        }
        NodeList rgb = arrElement.getElementsByTagName("integer");
        String r = rgb.item(0).getFirstChild().getNodeValue();
        String g = rgb.item(1).getFirstChild().getNodeValue();
        String b = rgb.item(2).getFirstChild().getNodeValue();
        int color = Color.rgb(Integer.parseInt(r), Integer.parseInt(g), Integer.parseInt(b));
        return color;
    }


    public String getSkinName(File file) throws ParserConfigurationException, SAXException, IOException {
        String result = null;
        if (file == null) return null;
        InputStream is = new FileInputStream(file);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = is.read(bytes)) > 0) {
            bos.write(bytes, 0, len);
            bos.flush();
        }
        String content = bos.toString("utf-8");
        is.close();
        bos.close();
        content = SkinUtil.filter(content);
        ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes("utf-8"));
        Document doc = builder.parse(bis);
        NodeList list = doc.getElementsByTagName("key");
        for (int i = 0; i < list.getLength(); i++) {
            Element element = (Element) list.item(i);
            String key = element.getFirstChild().getNodeValue();
            if (key.equals("ThemeName")) {
                Element theme = (Element) doc.getElementsByTagName("string").item(0);
                result = theme.getFirstChild().getNodeValue();
                break;
            }
        }
        bis.close();
        return result;
    }

    public Bitmap getSkinIcon(String filePath) {
        if (StringUtil.isEmpty(filePath)) return null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeFile(filePath, options);
    }

    private Drawable[] tabButtonBg(float[] radii) {
        int begin = Color.argb(0xF9, 0x00, 0x00, 0x00);
        int end = Color.argb(0xC1, 0x00, 0x00, 0x00);

        GradientDrawable gdNormal = new GradientDrawable(
                GradientDrawable.Orientation.BOTTOM_TOP,
                new int[]{begin, end});
        gdNormal.setCornerRadii(radii);
        gdNormal.setGradientType(GradientDrawable.LINEAR_GRADIENT);

        begin = Color.argb(0xB3, 0x00, 0x00, 0x00);
        end = Color.argb(0x7B, 0x50, 0x50, 0x50);

        GradientDrawable gdPressed = new GradientDrawable(
                GradientDrawable.Orientation.BOTTOM_TOP,
                new int[]{begin, end});
        gdPressed.setCornerRadii(radii);
        gdPressed.setGradientType(GradientDrawable.LINEAR_GRADIENT);

        Drawable[] drawables = new Drawable[2];
        drawables[0] = gdNormal;
        drawables[1] = gdPressed;

        return drawables;
    }

    public void destroyVerty() {

    }

    public void destroyHoriz() {

    }

    public void destroyGloble() {

    }

    public void destroyAllSkin() {
        destroyHoriz();
        destroyVerty();
        destroyGloble();
    }

    public void destoryMusic(SkinCommonData data) {
        data.hasmusic = Constants.SOUND_CLOSE;
        //: vert keyboard
//            if (player != null) {
//                player.destory();
//            }
        try {
            for (int i = 0; i < data.def_t9mp3.length; i++) {
                if (data.def_t9mp3[i] != null) {
                    data.def_t9mp3[i].close();
                    data.def_t9mp3[i] = null;
                }

            }

            for (int i = 0; i < data.def_abcmp3.length; i++) {
                if (data.def_abcmp3[i] != null) {
                    data.def_abcmp3[i].close();
                    data.def_abcmp3[i] = null;
                }
            }

            if (data.def_lock != null) {
                data.def_lock.close();
                data.def_lock = null;
            }
            if (data.def_tink != null) {
                data.def_tink.close();
                data.def_tink = null;
            }
            if (data.def_tock != null) {
                data.def_tock.close();
                data.def_tock = null;
            }
            if (data.def_ussd != null) {
                data.def_ussd.close();
                data.def_ussd = null;
            }
            if (data.def_pound != null) {
                data.def_pound.close();
                data.def_pound = null;
            }
            if (data.def_star != null) {
                data.def_star.close();
                data.def_star = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
